﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.IO;
using HTAProDataImporter.Transport;

namespace HTAProDataImporter.DataProvider
{
    public class HTAProProvider
    {
        protected static OleDbConnection NewConn
        {
            get
            {
                return new OleDbConnection(ConnString);
            }
        }

        private static string ConnString
        {
            get
            {
                return string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}", Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "HTAProData.mdb"));
            }
        }

        protected static void ExecuteNonQuery(OleDbCommand cmd)
        {
            using (OleDbConnection conn = NewConn)
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.ExecuteNonQuery();
                conn.Close();
            }
        }

   
        public static void GetCollection(BaseEntityTransport Parser)
        {
            using (OleDbConnection conn = NewConn)
            {
                conn.Open();
                using (OleDbCommand cmd = new OleDbCommand() {CommandType = System.Data.CommandType.Text, CommandText = Parser.CommandText})
                {
                    cmd.Connection = conn;
                    using (OleDbDataReader dr = cmd.ExecuteReader())
                    {
                        while (dr.Read())
                            Parser.InstanceFromReader(dr);

                        dr.Close();
                    }

                }
                conn.Close();
            }

        }
    }
}
